package com.hobbit.ui.managers
{
	import com.hobbit.ui.data.UiLayoutData;
	import com.hobbit.ui.viewbase.BaseView;
	import com.hobbit.utils.ClassUtil;
	
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	
	import starling.display.DisplayObject;

	public class PanelManager
	{
		public function PanelManager()
		{
			if(_instance == null)
			{
				_instance = this;
				initDic();
			}else
			{
				throw new Error("实例化单例类出错---> PanelManager");
			}
			
		}
		private static var _instance:PanelManager;
		public static function getInstance():PanelManager
		{
			if(_instance == null)
			{
				_instance = new PanelManager();
			}
			return _instance;
		}
		
		private function initDic():void
		{
			panelDic = new Dictionary();
		}
		
		
		/**
		 * 总字典 
		 */
		private var panelDic:Dictionary;
		
		
		/**
		 * 获取面板
		 * @param $panelClass
		 * @return 
		 * 
		 */
		public function getPanel($panelClass:Class):BaseView
		{
			
			if(panelDic[$panelClass])
			{
				return panelDic[$panelClass];
			}else
			{
				return createPanel($panelClass);
			}
		}
		/**
		 * 如果字典里没有改面板信息 存入面板信息
		 * @param $panelClass
		 * @return 
		 */
		private function createPanel($panelClass:Class):BaseView
		{
			var panel:Object = new $panelClass();
			UiManager.getInstance().layoutView(panel);
			
			//错误提示框不存入字典
			if((panel as BaseView).layoutData.type != UiLayoutData.ERROR_PANEL)
			{
				panelDic[$panelClass] = panel;
			}
			return panel as BaseView;
		}
		
		/**
		 * 注册一个panel，用于不通过config,而自己写的BaseView --key为他自己本身
		 */
		public function registerPanel($panel:BaseView):void
		{
			var className:String = ClassUtil.getClassName($panel);
			
			//错误提示框不存入字典
			if(($panel as BaseView).layoutData.type != UiLayoutData.ERROR_PANEL)
			{
				panelDic[className] = $panel;
			}
		}
	
		
	}
}